package com.cross.test;

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import com.cross.core.Reader;
import com.cross.core.SimpleTractor;
import com.cross.core.Writer;
import com.cross.object.SimpleObjectReader;
import com.cross.object.SimpleObjectWriter;

public class SimpleObjectReaderWriterTestCase extends TestCase {

	/**
	 * 新建立一个Person列表，通过DataCross的Java对象读取器SimpleObjectReader
	 * 和Java对象写出器SimpleObjectWriter这一对读写操作,
	 * 将源列表source中的数据复制到目标列表dest中,
	 * 并检查每个源列表中的对象在目标列表中是否存在。
	 * 
	 * @date 2011-7-7
	 * @author wuliuhua
	 */
	public void testWrite() {

		List<Person> source = new ArrayList<Person>();
		List<Person> dest = new ArrayList<Person>();
		
		for(int i=0; i<10; i++){
			source.add(new Person("person"+i, i));
		}
		
		try {
			Reader reader = new SimpleObjectReader(Person.class, source);
			Writer writer = new SimpleObjectWriter<Person>(dest, reader.getHeader(), Person.class);
			new SimpleTractor(reader, writer).start();
			
			super.assertTrue(source.size() == dest.size());
			
			boolean flag = false;
			for(Person sp : source){
				flag = true;
				for(Person dp : dest){
					if(sp.equals(dp)){
						flag = false;
					}
				}
				//在目标列表中找不到时flag = true;
				super.assertFalse(flag);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
//		for(Person sp : dest){
//			System.out.println(sp.toString());
//		}
	}
}
